package com.agent;
import java.awt.Component;
import java.awt.Container;
import java.awt.Window;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleText;
import javax.swing.*;
import javax.swing.text.JTextComponent;

import com.control.AbtControl;
import com.control.AbtLabel;

public class GetInformation {
	
	public static String DELIMITER = "@##@";
	public static Map<String, Component> controls  = new HashMap<String, Component>();
	public String title;
	
	public String getChildren(String title){
		controls.clear();
		this.title = title;
		String children = "";
		System.out.println("[huyngo] getChildren");
		
		Component comp = getComponent(title);
		System.out.println("comp: " + comp);
		
		if(comp != null && comp instanceof Container){
			Container compContainer = (Container)comp;
			Component[] comps = compContainer.getComponents();
			System.out.println("comps.size: " + comps.length);
			for(int i = 0; i < comps.length; i++){
				System.out.println(comps[i].toString());
				//String content ="";
				if(comps[i] instanceof JRootPane){
					Container panel = ((JRootPane)comps[i]).getContentPane();
					children += getChildOfContainer(panel);
				} else {
					children += "[" + comps[i].hashCode() + "]" + "[" + comps[i].getClass().getName() + "]" + DELIMITER;
					controls.put("[" + comps[i].hashCode() + "]" + "[" + comps[i].getClass().getName() + "]", comps[i]);
				}
			}
		}
		return children;
	}
	
	public String getChildOfContainer(Container container){
		String children ="";
		if(container instanceof JComponent){
			System.out.println(">>>>>>>>>> vao day roi ===============");
			
			JComponent parent = (JComponent)container;
			for(Component c : parent.getComponents()){
				String text= "";
				;
				if(c instanceof JTextComponent){
					text = ((JTextComponent)c).getName();
				} else if (c instanceof JLabel){
					text = ((JLabel)c).getText();
				} else if (c instanceof JPanel){
					text = ((JPanel)c).getName();
				} else if (c instanceof JButton){
					text = ((JButton)c).getText();
				} else if (c instanceof Box){
					text = ((Box)c).getName();
				}
				else {
					AccessibleText context = c.getAccessibleContext().getAccessibleText();
					if(context != null){
						text = context.toString();
					}
				}
				
				if(text==null || text.equals("null")){
					text ="";
				}
				
				controls.put("[" + c.hashCode() + "]" + "[" + c.getClass().getName() + "]" + text, c);
				
				children += "[" + c.hashCode() + "]" + "[" + c.getClass().getName() + "]" + text + DELIMITER;
				children += getChildOfContainer((Container)c);
			}
		}
		return children;
		
	}
	
	public String getProperties(String title, String control){
		String properties = "";
		System.out.println("========GET SUPPORTED PROPERTY============");
		System.out.println(control);
		
		if(controls!=null && controls.size()>0){
			if(controls.containsKey(control)){
				Component comp = controls.get(control); 
				System.out.println("[HUYNGO] ==== comp:" + comp.getClass().getName());
				
				// get class object
				try {
					Class<?> classObject = Class.forName(comp.getClass().getName());
					
					Field[] fields = classObject.getFields();
					
					for(Field field : fields){
						field.setAccessible(true);
						properties += field.getName().toLowerCase() + DELIMITER;
					}
//					AbtControl obj = null;
//					
//					if(comp instanceof JLabel){
//						obj = new AbtLabel(comp);
//					} else {
//						obj = new AbtControl(comp);
//					}
//					
//					List<String> props = obj.getPropertiesList();
//					
//					System.out.println(props);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("========= PROPERTIES: " + properties);
		return properties;
	}
	
	public String getProperty(String title, String control, String propertyName){
		String value = "";
		System.out.println("========GET PROPERTY============");
		System.out.println(control + " ==== " + propertyName);
		
		if(controls!=null && controls.size()>0){
			if(controls.containsKey(control)){
				Component comp = controls.get(control); 
				System.out.println("[HUYNGO] ==== comp:" + comp.getClass().getName());
				
				// get class object
				try {
					Class<?> classObject;
					
					classObject = Class.forName(comp.getClass().getName());
					
					Field field = classObject.getField(propertyName);
				
					if(field!=null){
						field.setAccessible(true);
						Object propertyValue = field.get(comp);
						if(propertyValue != null){
							value = propertyName.toString();
						}
					}
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NoSuchFieldException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
			}
		}
		
		System.out.println("========= VALUE: " + value);
		return value;
	}
	
	public Component getComponent(String title){
		Component comp = null;
		Window window = null;
		Window[]  windowArray = Window.getWindows();
		System.out.println("===[huyngo]=== windowArray: " + windowArray.length);
		if(windowArray!=null){
			for(int i=0;i<windowArray.length;i++){
				if(windowArray[i]!=null){
//					System.out.println("===[huyngo]=== windowArray[i]: " + windowArray[i]);
//					System.out.println("===[huyngo]=== windowArray[i].isActive: " + windowArray[i].isActive());
//					System.out.println("===[huyngo]=== windowArray[i].isDisplayable: " + windowArray[i].isDisplayable());
					//if(windowArray[i].isActive() && windowArray[i].isDisplayable()){
						String winTitle = windowArray[i].getName();
						AccessibleContext c = windowArray[i].getAccessibleContext();
						if(c != null){
							winTitle = c.getAccessibleName();
						}	
						System.out.println("===[huyngo]=== winTitle: " + winTitle);
						if(winTitle.equalsIgnoreCase(title)){
							window = windowArray[i];
							break;
						}
					//}
				}
			}
		}
		if(window!=null){
			comp = window;
		}
		return comp;
	}
	
	
	
}
